import path from "path";
import { getCmdParameter } from "../../common/src/utils/CmdParameter";
import { Main as devMain } from "../src/Main";

class Unit {
  constructor() {
    console.log("starting unit test...");
    // this.distTest();
    // this.mainTest();
    const foo = getCmdParameter("func")
    if (this.getAllMethods().indexOf(foo) > -1)
      this[foo as keyof Unit]()
    else {
      console.log('unknown function name: ' + foo, this.getAllMethods());
      console.log(`usage: node main.js -func useComponent -rootPath E:\lowCode\dist -componentName head`);
    }


    // this.loadTest();
    console.log("unit test end...");
  }

  private getAllMethods() {
    return Object.getOwnPropertyNames(Unit.prototype)
      .filter(prop => typeof Unit.prototype[prop as keyof Unit] === 'function');
  }

  private getParameter() {
    const obj: {
      [objName: string]: string
    } = {}
    for (let i = 0; i < process.argv.length; i += 2) {
      obj[process.argv[i].slice(1)] = process.argv[i + 1]
    }

    return obj
  }

  public clearPreview() {
    const parameter = this.getParameter()
    const rootPath = parameter['rootPath']

    new devMain(rootPath, true)
  }

  async useComponent() {
    // const rootPath = "D:/Desktop/trash/lowCode/dist";

    // const rootPath = path.resolve(getCmdParameter("root"))
    // const componentName = getCmdParameter("componentName")
    const parameter = this.getParameter()
    const rootPath = parameter['rootPath']
    const componentName = parameter['componentName']

    // console.log(rootPath, componentName);

    const myMain = new devMain(rootPath, false);
    myMain.dev();
    //load component ...
    const com = myMain.loadComponent(componentName);
    const componentInstance = new com.module(); //head component class from reflection
    const runtime = myMain.runComponent(com.componentInfo, componentInstance);
    // console.log(runtime.getJs());
    // console.log(runtime.getCss());
    // console.log(runtime.getDom().html());
    const impl = myMain.implementComponent(runtime);
    myMain.setConfigs(componentInstance, impl, runtime);
    //load component ...
    // configable.applyChange();
    myMain.beautify();

    // myMain.sync();
    // setInterval(() => {
    //   myMain.sync();
    // }, 2000);
  }
  loadTest() {
    // const ci = new ComponentInfo("head");
    // const lc = new LoadComponent(ci);
    // const module = lc.loadComponent();
    // const dryrun = new RunComponent(
    //   ci,
    //   module,
    //   IdGenerator.gen(),
    //   new CreateDist().getDistStructure()
    // );
  }
  distTest() {
    // const t = new CreateDist();
  }
}

new Unit();
